home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / jobs ƒ / jobs.c < prev    next >
C/C++ Source or Header  |  1998-06-21  |  5KB  |  164 lines

  1. /****************************************************************************************
  2.     jobs.cp
  3.     
  4.     Copyright © 1998 Red Shed Software. All rights reserved.
  5.     by Jonathan 'Wolf' Rentzsch (jon@redshed.net)
  6.     
  7.     You'll need the dcmdGlue.a.o object file and Dcmd.h header file to compile this code.
  8.     You can find it on the Developer Series Tool Chest CD in the MacsBug folder or maybe
  9.     on the web.
  10.     
  11.     Commenter    Date                Comment
  12.     ---------    -----------------    -----------------------------------------------------
  13.     wolf        Fri, Jun 19, 1998    Created ( at 2:55:24 AM at MacHack 13 ).
  14.     
  15.     ************************************************************************************/
  16.  
  17. #include    "Dcmd.h"
  18. #include    "A4Stuff.h"
  19. #include    "LowMem.h"
  20.  
  21. /*    Here's the undocumented Progress Manager call that's key to our dcmd */
  22.         extern    pascal    OSErr
  23. KillProcess(    const    ProcessSerialNumber    *PSN )    =    {    0x3F3C,
  24.                                                             0x0044,
  25.                                                             0xA88F };
  26.  
  27. /**************************
  28. *    
  29. *    Funky Protos
  30. *    
  31. **************************/
  32.  
  33. void    dcmdHandleHelp();
  34. void    dcmdHandleGetInfo( dcmdBlock *paramPtr );
  35. void    dcmdHandleDoIt();
  36.  
  37. /****************************************************************************************
  38.     Commenter    Date                Comment
  39.     ---------    -----------------    -----------------------------------------------------
  40.     wolf        Tue, Aug 12, 1997    Created.
  41.     wolf        Wed, Nov 5, 1997    Broke down monolithic main() into separate functions.
  42.     
  43.     ************************************************************************************/
  44.  
  45.     pascal    void
  46. main(        dcmdBlock    *paramPtr )
  47. {
  48.     long    oldA4 = SetCurrentA4();
  49.     
  50.     switch( paramPtr->request ) {
  51.         case dcmdHelp:
  52.             dcmdHandleHelp();            
  53.             break;
  54.         case dcmdGetInfo:
  55.             dcmdHandleGetInfo( paramPtr );
  56.             break;
  57.         case dcmdDoIt:
  58.             dcmdHandleDoIt();
  59.             break;
  60.         default:
  61.             break;
  62.     }
  63.     
  64.     SetA4(oldA4);
  65. }
  66.  
  67. /****************************************************************************************
  68.     Commenter    Date                Comment
  69.     ---------    -----------------    -----------------------------------------------------
  70.     wolf        Sat, Jun 20, 1998    Created.
  71.     
  72.     ************************************************************************************/
  73.  
  74.     void
  75. dcmdHandleHelp()
  76. {
  77.     dcmdDrawLine("\pKills every process except the Finder.");
  78. }
  79.  
  80. /****************************************************************************************
  81.     Commenter    Date                Comment
  82.     ---------    -----------------    -----------------------------------------------------
  83.     wolf        Sat, Jun 20, 1998    Created.
  84.     
  85.     ************************************************************************************/
  86.  
  87.     void
  88. dcmdHandleGetInfo(    dcmdBlock    *paramPtr )
  89. {
  90.     dcmdFillVersion( paramPtr, 0x03008000 );
  91.     dcmdFillString( paramPtr, usageStr, "\pnada (kills every process save the Finder)" );
  92.     dcmdFillString( paramPtr, creditsStr, "\pby Jonathan ‘Wolf’ Rentzsch (jon@redshed.net)" );
  93. }
  94.  
  95. /****************************************************************************************
  96.     Commenter    Date                Comment
  97.     ---------    -----------------    -----------------------------------------------------
  98.     wolf        Sat, Jun 20, 1998    Created.
  99.     
  100.     ************************************************************************************/
  101.  
  102.     void
  103. dcmdHandleDoIt()
  104. {
  105.     static    Str255            kZombies[]    =    {    "\pCopland",
  106.                                                 "\pNewton",
  107.                                                 "\pOpenDoc",
  108.                                                 "\pRhapsody",
  109.                                                 "\pTaligent",
  110.                                                 "\pPowerTalk",
  111.                                                 "\pQuickDraw GX",
  112.                                                 "\pDylan" };
  113.     
  114.     Str255                     string, name = "\p";
  115.     ProcessSerialNumber        psn = { kNoProcess, kNoProcess };
  116.     ProcessInfoRec            info;
  117.                             //    Random() crashes at MacsBug time so we use the low
  118.                             //    three bits of TickCount() for our random index.
  119.     short                    r = ( TickCount() & 0x00000007 );
  120.     OSErr                    err = noErr;
  121.     
  122.     info.processInfoLength = sizeof( info );
  123.     info.processName = name;
  124.     info.processAppSpec = nil;
  125.     
  126.     if( !err ) {
  127.         err = GetCurrentProcess( &psn );
  128.     }
  129.     if( !err ) {
  130.         err = GetProcessInformation( &psn, &info );
  131.     }
  132.     if( !err && info.processSignature != 'MACS' ) {
  133.         dcmdDrawLine( "\pThe Finder has to be the current process in order not to crash MacsBug" );
  134.         return;
  135.     }
  136.     if( !err ) {
  137.         err = GetFrontProcess( &psn );
  138.     }
  139.     dcmdDrawLine( "\pWe must focus on the Mac OS to move forward.\nAll other processes are extraneous and must be killed." );
  140.     psn.highLongOfPSN = psn.highLongOfPSN = kNoProcess;
  141.     while( !err ) {
  142.         if( !err ) {
  143.             err = GetProcessInformation( &psn, &info );
  144.         }
  145.         if( !err && info.processSignature != 'MACS' ) {
  146.             err = KillProcess( &psn );
  147.             if( !err ) {
  148.                 BlockMoveData( err ? "\pjobs:    error #" : "\pkilled process “", string, 17 );
  149.                 if( err ) NumToString( err, name );
  150.                 BlockMoveData( name + 1, string + 1 + string[ 0 ], name[ 0 ] );
  151.                 string[ 0 ] += name[ 0 ];
  152.                 if( !err ) string[ ++string[ 0 ] ] = '”';
  153.                 dcmdDrawLine( string );
  154.             }
  155.         }
  156.         if( !err )
  157.             err = GetNextProcess( &psn );
  158.     }
  159.     BlockMoveData( "\pkilled process “", string, 17 );
  160.     BlockMoveData( kZombies[ r ] + 1, string + 1 + string[ 0 ], kZombies[ r ][ 0 ] );
  161.     string[ 0 ] += kZombies[ r ][ 0 ];
  162.     string[ ++string[ 0 ] ] = '”';
  163.     dcmdDrawLine( string );
  164. }